﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_Model.Pay
{
    /// <summary>
    /// 获取订单支付金额请求参数
    /// </summary>
    public class PaymentAmountParams:IPayUser
    {
        /// <summary>
        /// 【必填】购买人ID
        /// </summary>
        [Required(ErrorMessage = "购买人ID不能为空")]
        public string userid { get; set; }

        /// <summary>
        /// 【必填】购买的商品信息
        /// </summary>
        [Required(ErrorMessage = "购买商品信息不能为空")]
        public List<PaymentAmountParamsCommodityInfo> CommodityIds;
    }

    /// <summary>
    /// 购买的商品信息
    /// </summary>
    public class PaymentAmountParamsCommodityInfo: IPayCommodityInfo, IValidatableObject
    {
        /// <summary>
        /// 【必填】购买商品ID
        /// </summary>
        [Required(ErrorMessage ="购买商品ID不能为空")]
        public Nullable<int> CommodityId { get; set; }
        /// <summary>
        /// 【必填】商品购买期限；0（无限期）、1（一个月）、3（三个月）、6（六个月）12（一年）
        /// </summary>
        [Required(ErrorMessage = "购买商品期限不能为空")]
        public Nullable<int> PurchasePeriod { get; set; }

        /// <summary>
        /// 规格列表
        /// <br/>
        /// 2021-9-16：禅道172号需求添加
        /// </summary>
        public List<SpecParamModel> Specs { get; set; }

        /// <summary>
        /// 商品数量，默认值=1（虚拟商品不需要设置数量）
        /// </summary>
        public int Quantity { get; set; }

        /// <summary>
        /// 参数校验
        /// </summary>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            PaymentAmountParamsCommodityInfo param = validationContext.ObjectInstance as PaymentAmountParamsCommodityInfo;

            if (param != null)
            {
                if (param.PurchasePeriod != null
                    && PurchasePeriodEnum.Permanent != param.PurchasePeriod
                    && PurchasePeriodEnum.OneMonth!=param.PurchasePeriod
                    && PurchasePeriodEnum.ThreeMonth != param.PurchasePeriod
                    && PurchasePeriodEnum.SixMonth != param.PurchasePeriod
                    && PurchasePeriodEnum.TwelveMonth != param.PurchasePeriod)
                {
                    yield return new ValidationResult("商品购买期限值无效，必须是：0、1、3、6、12其中之一", new string[] { "PurchasePeriod" });
                }
            }
        }
    }

    /// <summary>
    /// 购买商品ID、购买期限、价格
    /// </summary>
    public class PaymentAmountParamsCommodityDetailedInfo: PaymentAmountParamsCommodityInfo
    {
        /// <summary>
        /// 商品的价格
        /// </summary>
        public decimal OriPrice { get; set; }
    }
}
